package at.fhooe.im440.pandora.util;

public class CommandLineParser {
	  
	  private static final String HEIGHT_OPTION = "height";
	  private static final String WIDTH_OPTION = "width";
	  private final static int DEFAULT_WIDTH = 1680;
	  private final static int DEFAULT_HEIGHT = 1050;
	  private String[] args;

	  public CommandLineParser(String[] args) {
	    this.args = args;
	  }

	  public int height() {
	    return parseIntOption(HEIGHT_OPTION, DEFAULT_HEIGHT);
	  }

	  public int width() {
	    return parseIntOption(WIDTH_OPTION, DEFAULT_WIDTH);
	  }

	  private int parseIntOption(String option, int defaultValue) {
	    for (int i = 0; i < this.args.length; ++i) {
	      if (args[i].equalsIgnoreCase("-" + option) && i < args.length - 1) {
	        return parseInt(this.args[i + 1], defaultValue);
	      }
	    }
	    return defaultValue;
	  }

	  private int parseInt(String str, int fallBack) {
	    try {
	      return Integer.parseInt(str);
	    } catch (NumberFormatException e) {
	      return fallBack;
	    }
	  }
	}